<?php

namespace app\admin\controller;

use \think\Cache;
use \think\Controller;
use think\Loader;
use think\Db;
use \think\Cookie;
use \think\Session;
use app\admin\controller\Permissions;
use app\admin\model\Article as articleModel;
use app\admin\model\ArticleCate as cateModel;

class Kc extends Permissions
{
    //新闻列表
    public function index()
    {
        $info = \db('fit_kc')->order('kc_id desc')->paginate(10);

        $page = $info->render();
        $this->assign('page', $page);
        $this->assign('info', $info);
        return $this->fetch();
    }

    //添加新闻
    public function index_add()
    {
        if (request()->isPost()) {
            $content = input('content'); //添加的内容
            $kc_sort = input('kc_sort'); //排序
            $title = input('title');
            $time = time();
            $content_time = date("Y-m-d H:i:s", $time); //时间
            $data = [
                'kc_text' => $content,
                'kc_time' => $content_time,
                'kc_sort' => $kc_sort,
                'title' => $title
            ];
            $about = \db('fit_kc')->insert($data);
            if (!empty($about)) {
                $this->success('添加成功', 'kc/index');
            }
        }
        return $this->fetch();
    }

    //新闻的修改
    public function index_update()
    {
        if (request()->isPost()) {
            $id = input('id');
            $content = input('content');
            $kc_sort = input('kc_sort'); //排序
            $title = input('title');
            $time = time();
            $content_time = date("Y-m-d H:i:s", $time); //时间
            $about = \db('fit_kc')
                ->where('kc_id', $id)
                ->update([
                    'kc_text' => $content,
                    'kc_time' => $content_time,
                    'kc_sort' => $kc_sort,
                    'title' => $title
                ]);
            if ($about >= 0) {
                $this->success('修改成功', 'kc/index');
            } else {
                $this->error('修改失败', 'kc/index');
            }
        } else {
            $id = input('id');
            $info = \db('fit_kc')->where('kc_id', $id)->find();
            $this->assign('info', $info);
        }
        return $this->fetch();
    }

    //内容的删除
    public function index_delect()
    {
        $id = input('id');
        $about = \db('fit_kc')->where('kc_id', $id)->delete();
        if ($about > 0) {
            $this->success('删除成功', 'kc/index');
        } else {
            $this->error('删除失败', 'kc/index');
        }
    }
}
